package cn.kgc.commons.advice;

import cn.kgc.base.entity.SysLog;
import cn.kgc.base.mapper.SysLogMapper;
import cn.kgc.commons.annotation.DictLog;
import cn.kgc.commons.constant.SystemConstant;
import cn.kgc.commons.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2022年11月28日10:40
 * 记录用户的操作日志信息
 */
@Aspect
@Component
@Slf4j
@Transactional
public class DictAdvice {
    @Pointcut("@annotation(cn.kgc.commons.annotation.DictLog)")
    public void autoLogPointCut(){}

    @Resource
    private SysLogMapper sysLogMapper;

    @Around("autoLogPointCut()")
    public Object autoLogAround(ProceedingJoinPoint joinPoint)throws Throwable{
        //获得开始的执行时间
        long startTime = System.currentTimeMillis();
        //执行目标方法
        Object result = joinPoint.proceed();
        //获得结束的执行时间
        long endTime = System.currentTimeMillis();
        //用结束时间-开始时间 得到执行时间  获得消耗时间
        long costTime= endTime-startTime;
        saveLog(joinPoint,costTime);
        return result;
    }

    private void saveLog(ProceedingJoinPoint joinPoint,long costTime){

        //获得切入点的方法
        MethodSignature methodSignature =
                (MethodSignature) joinPoint.getSignature();

        Method method = methodSignature.getMethod();
        //获得方法上的注解信息
        DictLog dictLog = method.getAnnotation(DictLog.class);
        String logContent=dictLog.logContent();
        int logType = dictLog.logType().getLogType();
        String name = dictLog.moduleName().getName();
        int operateType=dictLog.operateType().getOperateType();

        //获得目标方法的类名
        String className = joinPoint.getTarget().getClass().getName();
        log.info("className:{}",className);  //cn.kgc.controller.PmsBrandController
        //获得目标方法的名字
        String methodName=className+""+method.getName();
        log.info("目标方法名字:{}",methodName);  //cn.kgc.controller.PmsBrandControllerselectAll
        //获得请求地址

        HttpServletRequest httpServletRequest = SpringContextUtils.getHttpServletRequest();
        String requestUrl = httpServletRequest.getRequestURL().toString();
        //todo  从请求中获得用户的ID和用户名
        Long userId=0L;
        String username="";
        //封装日志信息
        SysLog sysLog = SysLog.builder().costTime(costTime)
                .logContent(logContent)
                .method(methodName)
                .logType(logType).moudulename(name)
                .operateType(operateType).requestUrl(requestUrl)
                .userId(userId).userName(username).build();

        log.info("日志增强类获得的日志信息：{}",sysLog);
        sysLogMapper.insert(sysLog);

    }
}
